package com.jkl.controller.drTableDetail;

import com.alibaba.fastjson.JSONObject;
import com.jkl.entity.BaseResponseDTO;
import com.jkl.entity.drTableDetail.DrTableDetail;
import com.jkl.service.drTable.DrTableGenerateService;
import com.jkl.service.drTableDetail.inter.DrTableDetailServiceInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

import static com.jkl.service.drTable.DrTableGenerateService.TableIndexRequiredException;

@RestController
@RequestMapping("/jky/drTableDetail")
@Slf4j
public class DrTableDetailController {

	@Autowired
	private HttpServletRequest request;

	@Autowired
	DrTableGenerateService drTableGenerateService;

	@Autowired
	private DrTableDetailServiceInterface DrTableDetailServiceInterface;

	@PostMapping("/getDrTableDetailList")
	public List<DrTableDetail> getIndexList(@RequestBody  DrTableDetail drTableDetail) {
		BaseResponseDTO returnData = new BaseResponseDTO();
		List<DrTableDetail> drIndexList = DrTableDetailServiceInterface.queryDrTableDetail(drTableDetail);
		return drIndexList;

	}
	
	@PutMapping("/upDrTableDetail")
	public BaseResponseDTO upDate(@RequestBody DrTableDetail drTableDetail) {
		BaseResponseDTO returnData = new BaseResponseDTO();
		int drIndexInt = DrTableDetailServiceInterface.updateDrTableDetail(drTableDetail);
		if(drIndexInt!=0) {
			returnData.setResultMessage("修改成功");
		}else {
			returnData.setResultMessage("修改失败");
		}
		return returnData;

	}
	@PostMapping("/deletaDrTableDetail")
	public BaseResponseDTO deletaDrTableDetail(@RequestBody DrTableDetail drTableDetail) {
		BaseResponseDTO returnData = new BaseResponseDTO();
		int drIndexInt = DrTableDetailServiceInterface.deleteDrTableDetail(drTableDetail);
		if(drIndexInt!=0) {
			returnData.setResultMessage("删除成功");
		}else {
			returnData.setResultMessage("删除失败");
		}
		return returnData;
	}

	@GetMapping("/required/{tableId}")
	public BaseResponseDTO checkRequiredIndex(@PathVariable("tableId") Integer tableId){
		BaseResponseDTO responseDTO = new BaseResponseDTO();
		try{
			drTableGenerateService.checkBeforeReport(tableId);
			responseDTO.setResultCode("1");
			responseDTO.setResultMessage("添加成功");
		}catch (TableIndexRequiredException ex){
			responseDTO.setResultCode("0");
			responseDTO.setResultMessage(ex.getMessage());
		}
		return responseDTO;
	}

	@GetMapping("/reference/{tableId}")
	public BaseResponseDTO getReferenceIndex(@PathVariable("tableId") Integer tableId){
		BaseResponseDTO responseDTO = new BaseResponseDTO();
		try{
			Map<String,String> dataMap = drTableGenerateService.referenceIndex(tableId);
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("data",dataMap);
			responseDTO.setResultCode("1");
			responseDTO.setReturnData(jsonObject);
		}catch (Exception ex){
			responseDTO.setResultCode("0");
			responseDTO.setResultMessage(ex.getMessage());
		}
		return responseDTO;
	}

}